﻿using Sc.Domain;
using Sc.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;

namespace Sc.Web.Controllers
{
    public class DriverCertificateController : BaseController
    {
        /// <summary>
        /// 首页
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            ViewData["olist"] = CommService.GetOrgzByUserPer(UserLogin.UserId, 3);
            ViewData["type"] = new BaseItemService().GetListByType(7);
            return View();
        }
        /// <summary>
        /// 获取检查列表
        /// </summary>
        /// <param name="pageSize">每页显示条数</param>
        /// <param name="pageIndex">当前页</param>
        /// <returns></returns>
        public JsonResult GetPageData(int pageSize, int pageIndex)
        {
            var orgz = Convert.ToInt32(Request.Params["orgz"]);
            var baseItemId = Convert.ToInt32(Request.Params["baseItemId"]);
            var userName = Request.Params["uName"];
            var data = new DriverCertificateService().GetPageData(orgz, pageSize, pageIndex, baseItemId, userName);
            return Json(data, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 新增证书
        /// </summary>
        /// <returns></returns>
        public ActionResult CreateDriverCertificate()
        {
            ViewData["type"] = new BaseItemService().GetListByType(7);
            return View();
        }
        /// <summary>
        /// 修改证书
        /// </summary>
        /// <returns></returns>
        public ActionResult UpdateDriverCertificate()
        {
            ViewData["type"] = new BaseItemService().GetListByType(7);
            int id = Convert.ToInt32(Request.Params["id"]);
            ViewData["ent"] = new DriverCertificateService().GetModel(id);
            return View();
        }
        /// <summary>
        /// 证书详情
        /// </summary>
        /// <returns></returns>
        public ActionResult DriverCertificateDetail(int id)
        {
            ViewData["ent"] = new DriverCertificateService().GetModel(id);
            return View();
        }
        /// <summary>
        /// 保存数据
        /// </summary>
        /// <returns></returns>
        public int SaveData()
        {
            string data = Request.Params["data"];
            var filePaths = Request.Params["fp"].Split(',');
            JavaScriptSerializer json = new JavaScriptSerializer();
            DriverCertificate ent = json.Deserialize<DriverCertificate>(data);
            ent.UserNo = UserLogin.UserId;
            IList<Sys_Attachments> list = new List<Sys_Attachments>();
            for (int i = 0; i < filePaths.Length; i++)
            {
                if (!string.IsNullOrEmpty(filePaths[i]))
                {
                    Sys_Attachments att = new Sys_Attachments();
                    att.FileName = "司机证书图片";
                    att.FileUrl = filePaths[i];
                    att.ObjectName = "DCFE";
                    list.Add(att);
                }
            }
            return new DriverCertificateService().Add(ent, list);
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids">数据</param>
        /// <returns></returns>
        public int DeleteList(string ids)
        {
            return new DriverCertificateService().DeleteList(ids) ? 1 : 0;
        }
    }
}